<?php
	class User_Model extends CI_Model {
		function __construct()
		{
			parent::__construct();	
		}
		
		function add_supplier($member)
		{
			if($member['username'] != null) {
				$this->load->helper('security');
				$data = array(
					'role_id' => $member['role_id'] ,
					'gold_regis' => ($member['regis']) ,
					'email' => ($member['email']) ,
					'username' => $member['username'],
					'firstname' => $member['firstname'],
					'lastname' => $member['lastname'],
					'password' => $member['password'],
					'tel' => $member['tel'],
					'created' => $member['created'],
					'seckey' => $member['seckey'],
					'active' => 0
				); 
				$this->db->insert('users', $data);
				return $this->db->insert_id();
			}else{
				return 0;
			}
		}
		
		function add_gold($data)
		{
			if($data['user_id'] != null) {
				$this->load->helper('security');
				$data = array(
					'user_id' => $data['user_id'] ,
					'regis' => ($data['regis']) ,
					'active' => 0
				); 
				$this->db->insert('user_gold', $data);
				return $this->db->insert_id();
			}else{
				return 0;
			}
		}
		
		function add_company($company)
		{
			if($company['name'] != null) {
				$this->load->helper('security');
				$data = array(
					'user_id' => $company['user_id'] ,
					'name' => $company['name'] ,
					'main_cat_id' => $company['main_cat_id'] ,
					'cat_id' => ($company['cat_id']) ,
					'address' => $company['address'],
					'district' => $company['district'],
					'geo_id' => $company['geo_id'],
					'province' => $company['province'],
					'postcode' => $company['postcode'],
					'country' => $company['country'],
					'agent' => $company['agent'],
					'website' => $company['website'],
					'email' => $company['email'],
					'tel' => $company['tel'],
					'fax' => $company['fax'],
					'brand' => $company['brand'],
					'view' => 0,
					'active' => 1
				); 
				$this->db->insert('user_company_profile', $data);
				return $this->db->insert_id();
			}else{
				return 0;
			}
		}
		
		function getUserByEmail($email)
		{
			$query = $this->db->get_where('users', array('email' => trim($email)));
			return $query->row_array();
		}
		
		function getUserBySeccode($sec_code)
		{
			$query = $this->db->get_where('users', array('seckey' => trim($sec_code)));
			return $query->row_array();
		}
		
		function getUserByUsername($username)
		{
			$query = $this->db->get_where('users', array('username' => trim($username)));
			return $query->row_array();
		}
		
		function getUserInfo($user)
		{
			$this->db->where('username', $user); 
			$this->db->or_where('email', $user); 
			$query = $this->db->get('users');
			return $query->row_array();
		}
		
		function getUserInfoByID($id)
		{
			$this->db->where('id', $id); 
			$query = $this->db->get('users');
			return $query->row_array();
		}
		
		function update_company_img($input)
		{
			if($input['id'] != null) {
				$data = array(
					'img' => $input['img'] ,
				);
				$this->db->where('id', $input['id']);
				$this->db->where('user_id', $input['user_id']);
				$this->db->update('user_company_profile', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function update_company_logo($input)
		{
			if($input['id'] != null) {
				$data = array(
					'logo' => $input['logo'] ,
				);
				$this->db->where('id', $input['id']);
				$this->db->where('user_id', $input['user_id']);
				$this->db->update('user_company_profile', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function delete_user($id)
		{
                        $this->db->where('id', $id); 
                        if($this->db->delete('users')){
				if($this->delete_company($id,true)){
					return 1;
				}else{
					return 0;
				}
			}else{
				return 0;
			}
		}
		
		function delete_company($id,$all=false)
		{
                    if($all){
                        $this->db->where('user_id', $id);
			if($this->db->delete('user_company_profile')){
				return 1;
			}else{
				return 0;
			}
                    }else{
                        $this->db->where('id', $id);
			if($this->db->delete('user_company_profile')){
				return 1;
			}else{
				return 0;
			}
                    }
		}
		
		function email_confirm($sec_code)
		{
			$time = time();
			$data = array(
				'confirm' => 1 ,
				'modified' => $time,
				'seckey' => md5($time.rand(-999 , 999))
			);
			$this->db->where('seckey', $sec_code);
			$this->db->update('users', $data);
			return $this->db->affected_rows();
		}
		
		function update_user_forgotpass($user)
		{
			$time = time();
			$data = array(
				'modified' => $time,
				'password' => md5($user['password']),
				'seckey' => md5($time.rand(-999 , 999))
			);
			$this->db->where('seckey', $user['seckey']);
			$this->db->where('id', $user['id']);
			$this->db->update('users', $data);
			return $this->db->affected_rows();
		}
		
		function getUserLevel($id)
		{
			$query = $this->db->get_where('user_gold', array('user_id' => trim($id)));
			return $query->row_array();
		}
		
		function getCompanyInfo($id)
		{
			$this->db->where('user_id', $id); 
			$query = $this->db->get('user_company_profile');
			return $query->row_array();
		}
		
		function getCompanyWebsite($id)
		{
			$this->db->where('id', $id); 
			$query = $this->db->get('user_company_site');
			return $query->row_array();
		}
		
		function getCompanyWebsiteByCompanyId($id)
		{
			$this->db->where('company_id', $id); 
			$query = $this->db->get('user_company_site');
			return $query->row_array();
		}
		
		function update_supplier($member)
		{
			if($member['id'] != null) {
				$this->load->helper('security');
				$data = array(
					'role_id' => $member['role_id'] ,
					'firstname' => $member['firstname'],
					'lastname' => $member['lastname'],
					'tel' => $member['tel'],
					'modified' => $member['modified'],
					'seckey' => $member['seckey']
				);
				if($member['password']!=''){
					$data['password'] = md5($member['password']);
				}
				$this->db->where('id', $member['id']);
				$this->db->where('password', $member['cpassword']);
				$this->db->update('users', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function update_company($company)
		{
			if(isset($company['name'])) {
				//$this->load->helper('security');
				$data = array(
					'name' => $company['name'] ,
					'main_cat_id' => $company['main_cat_id'] ,
					'cat_id' => $company['cat_id'] ,
					'address' => $company['address'],
					'district' => $company['district'],
					'geo_id' => $company['geo_id'],
					'province' => $company['province'],
					'postcode' => $company['postcode'],
					'country' => $company['country'],
					'agent' => $company['agent'],
					'website' => $company['website'],
					'email' => $company['email'],
					'tel' => $company['tel'],
					'fax' => $company['fax'],
					'brand' => $company['brand'],
					'c_modified' => $company['c_modified']
				);
				
				$this->db->where('id', $company['id']);
				$this->db->update('user_company_profile', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function update_company2($input=FALSE , $id=FALSE)
		{
			if($input AND $id) {
				$this->db->where('id', $id);
				$this->db->update('user_company_profile', $input);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function regis_gold($user_id,$time)
		{
			if($user_id) {
				$this->load->helper('security');
				$data = array(
					'gold_regis' => 1,
					'modified' => $time,
					'seckey' => md5($time.rand(-999 , 999))
				);
				
				$this->db->where('id', $user_id);
				$this->db->update('users', $data);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
		
		function save_website($input=array())
		{
			if(is_array($input) AND count($input)>0) {
				$this->load->helper('security');
				$this->db->insert('user_company_site', $input);
				return $this->db->insert_id();
			}else{
				return 0;
			}
		}
		
		function update_website($input=array() , $id=NULL)
		{
			if(is_array($input) AND count($input)>0 AND $id!==NULL) {
				$this->db->where('id', $id);
				$this->db->update('user_company_site', $input);
				return $this->db->affected_rows();
			}else{
				return 0;
			}
		}
	}


/* End of file user_model.php */
/* Location: ./application/modules/user/models/user_model.php */